﻿<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Admin_manageNews.aspx.cs" Inherits="scmsByAspdNet.Admin_manageNews" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>新闻管理</title>
    <link rel="stylesheet" href="static/bootstrap-4.6.0/css/bootstrap.css" />
    <link rel="stylesheet" href="https://unpkg.com/bootstrap-table@1.15.3/dist/bootstrap-table.min.css" />
    <script src="static/js/jquery-3.6.0.js"></script>
    <script src="static/js/jquery-3.6.0.min.js"></script>
</head>
<body>
    <%-- 导航栏 --%>
    <div class="nav-warp">
        <nav class="navbar navbar-expand-lg navbar-dark bg-dark">
            <a class="navbar-brand" href="Admin.aspx">学生选课系统-管理员界面</a>
            <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarSupportedContent" aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
                <span class="navbar-toggler-icon"></span>
            </button>

            <div class="collapse navbar-collapse" id="navbarSupportedContent">
                <ul class="navbar-nav mr-auto">
                    <li class="nav-item dropdown">
                        <a class="nav-link dropdown-toggle" href="#" id="navbarDropdown" role="button" data-toggle="dropdown" aria-expanded="false">更多功能
                        </a>
                        <div class="dropdown-menu" aria-labelledby="navbarDropdown">
                            <a class="dropdown-item" href="#">相关信息</a>
                            <a class="dropdown-item" href="#">关于我们</a>
                            <div class="dropdown-divider"></div>
                            <a class="dropdown-item" href="#">版本有关</a>
                        </div>
                    </li>
                </ul>
                <ul class="navbar-nav ml-auto">
                    <li class="nav-item">
                        <a id="sign-out" class="nav-link" href="./Login.aspx" tabindex="-1" aria-disabled="true" style="color: red;">退出登录</a>
                    </li>
                </ul>
                <form class="form-inline my-2 my-lg-0">
                    <input class="form-control mr-sm-2" type="search" placeholder="没有任何功能" aria-label="Search" />
                    <button class="btn btn-outline-success my-2 my-sm-0 loginSubmit" type="submit">Search</button>
                </form>
            </div>
        </nav>
    </div>
    <%-- 新闻表格 --%>
    <div id="tb">
        <button type="button" class="btn btn-success" data-toggle="modal" data-target="#add_news_modal">添加新闻</button>
    </div>
    <div style="width: 1200px; margin: 0 auto">
        <table id="news-manage-table" style="max-width: 1200px;">
        </table>
    </div>
    <%-- 添加新闻模态框 --%>
    <!-- Modal -->
    <div class="modal fade" id="add_news_modal" tabindex="-1" aria-labelledby="exampleModalLabel" aria-hidden="true">
        <div class="modal-dialog">
            <div class="modal-content">
                <div class="modal-header">
                    <h5 class="modal-title" id="exampleModalLabel">Modal title</h5>
                    <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                        <span aria-hidden="true">&times;</span>
                    </button>
                </div>
                <div class="modal-body">
                    <form>
                        <div class="form-group">
                            <label for="news_title">新闻标题</label>
                            <input type="text" class="form-control" id="news_title" />
                        </div>
                        <div class="form-group">
                            <label for="news_detail">新闻内容</label>
                            <textarea class="form-control" id="news_detail" rows="3"></textarea>
                        </div>
                        <div class="form-group">
                            <label for="news_image">默认封面</label>
                            <select class="form-control" id="news_image">
                                <option value="hubu1.jpg">封面1</option>
                                <option value="hubu2.jpg">封面2</option>
                                <option value="hubu3.jpg">封面3</option>
                            </select>
                        </div>
                    </form>
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-secondary" data-dismiss="modal">关闭</button>
                    <button type="button" class="btn btn-primary" onclick="news_manage_table_op('add_news',0)">提交</button>
                </div>
            </div>
        </div>
    </div>
</body>
<script src="static/bootstrap-4.6.0/js/bootstrap.js"></script>
<script src="https://unpkg.com/bootstrap-table@1.15.3/dist/bootstrap-table.min.js"></script>
<script>
    let $news_manage_table;
    init_news_manage_table();
    function init_news_manage_table() {
        $news_manage_table = $('#news-manage-table').bootstrapTable({
            url: 'cotro/Admin/getNews.ashx',
            method: "post",
            search: true, //是否显示表格搜索
            sidePagination: "client", //分页方式：client客户端分页，server服务端分页（*）
            pageNumber: 1,
            pageSize: 5, //每页的记录行数（*）
            pageList: [10, 25, 50, 100], //可供选择的每页的行数（*）
            pagination: true, //是否显示分页（*）
            height: 560,
            toolbar: "#tb",
            singleSelect: false,
            contentType: "application/x-www-form-urlencoded", //发送给服务器的编码类型
            columns: [{
                field: 'id',
                title: '新闻号',
                width: 100,
            }, {
                field: 'title',
                title: '新闻标题',
                width: 200,
            }, {
                field: 'detail',
                title: '内容',
                width: 600,
            }, {
                field: 'is_open',
                title: '是否开启',
                width: 100,
            }, {
                field: 'id',
                title: '可供操作',
                width: 200,
                formatter: function (value, row, index) {
                    return news_manage_table_formatter(value, row, index, 'offer')
                },
            },]
        });
    }

    function news_manage_table_formatter(value, row, index, op) {
        let result = ``;
        if (op === "offer") {
            if (row.is_open == 0) {
                result = `<button type="button" class="btn btn-success" onclick="news_manage_table_op('open_news',${row.id})">打开新闻</button>`
            }
            else {
                result = `<button type="button" class="btn btn-danger" onclick="news_manage_table_op('close_news',${row.id})">关闭新闻</button>`
            }
        }
        return result;
    }

    function news_manage_table_op(...value) {
        console.log(value);
        if (value[0] == "open_news") {
            $.post("cotro/admin/updateNews.ashx", {
                id: value[1],
                is_open: 1
            }, function (res, status) {
                if (status === "success") {
                    res = JSON.parse(res);
                    if (res.line == 1) {
                        alert("修改成功")
                        $news_manage_table.bootstrapTable('refresh')
                    } else {
                        alert("修改失败")
                    }
                }
            })
        }
        else if (value[0] == "close_news") {
            $.post("cotro/admin/updateNews.ashx", {
                id: value[1],
                is_open: 0
            }, function (res, status) {
                if (status === "success") {
                    res = JSON.parse(res);
                    if (res.line == 1) {
                        alert("修改成功")
                        $news_manage_table.bootstrapTable('refresh')
                    } else {
                        alert("修改失败")
                    }
                }
            })
        }
        else if (value[0] == "add_news") {

            $.post("cotro/admin/addNews.ashx", {
                title: $("#news_title").val(),
                detail: $("#news_detail").val(),
                img_name: $("#news_image").val(),
            }, function (res, status) {
                if (status === "success") {
                    res = JSON.parse(res);
                    if (res.line == 1) {
                        alert("添加成功");
                        $news_manage_table.bootstrapTable('refresh')
                    } else {
                        alert("添加失败");
                    }
                }
            })
        }
    }
</script>
</html>
